package com.gf.forcesystem.mq;

import com.gf.forcesystem.config.CotaskingRabbitmqConfig;
import com.gf.forcesystem.model.dto.UAVState;
import com.gf.forcesystem.api.UavStateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gaofei
 * @date 2022/6/19 16:49
 * 无人机状态
 */
@Component
@Slf4j
public class UavStateMqReciver {
    @Resource
    List<UavStateService> uavStateServices;

    @RabbitListener(queues = CotaskingRabbitmqConfig.uavStateQueue)
    @RabbitHandler
    public void recv(byte[] data) {
        UAVState uavState;
        try {
            uavState = UAVState.loadFromBytes(data);
        } catch (Exception e) {
            log.warn("UavStateMqReciver 收到无法解析的数据: {}", data);
            return;
        }

        if (CollectionUtils.isEmpty(uavStateServices)) {
            return;
        }

        for (UavStateService uavStateService : uavStateServices) {
            try {
                uavStateService.deal(uavState);
            } catch (Exception e) {
                log.error("{} 处理无人机状态帧出错", uavStateService.getClass().getName(), e);
            }
        }
    }
}
